//
//  CustomPopoverView.m
//  WEIMO
//
//  Created by Lixiaoyong on 13-7-24.
//  Copyright (c) 2013年 广州从兴电子开发有限公司. All rights reserved.
//

#import "CustomPopoverView.h"

#import <QuartzCore/QuartzCore.h>
#import "DLog.h"

#define kArrowHeight 10.0
#define kCornerRadius 8.0
#define kCellHeight 34.0

@interface CustomPopoverView ()
@property (nonatomic,retain) UITableView *popoverTableView;
@end

@implementation CustomPopoverView
@synthesize tableViewDatas;
@synthesize popoverTableView;
@synthesize delegate;
@synthesize isShowOrHide;

- (void)dealloc
{
    self.popoverTableView = nil;
    self.tableViewDatas = nil;
    self.delegate = nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        
        self.popoverTableView = [[[UITableView alloc] initWithFrame:CGRectZero] autorelease];
        self.popoverTableView.dataSource = self;
        self.popoverTableView.delegate = self;
        self.popoverTableView.backgroundColor = [UIColor clearColor];
        self.popoverTableView.separatorColor = [UIColor colorWithRed:41/255.0 green:49/255.0 blue:60/255.0 alpha:1.0];
        [self addSubview:self.popoverTableView];
        
        self.isShowOrHide = NO;
        self.tableViewDatas = [[[NSMutableArray alloc] init] autorelease];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    float ww = rect.size.width - kCornerRadius*2 - kArrowHeight*2;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 3.0);
    CGContextSetRGBStrokeColor(context, 75/255.0, 75/255.0, 76/255.0, 1.0);
    
    CGContextSetRGBFillColor(context, 44/255.0, 53/255.0, 62/255.0, 1.0);
    
    CGContextMoveToPoint(context, kCornerRadius, kArrowHeight);
    CGContextAddLineToPoint(context, kCornerRadius + ww, kArrowHeight);
    CGContextAddLineToPoint(context, kCornerRadius + ww + kArrowHeight, 0);
    CGContextAddLineToPoint(context, kCornerRadius + ww + kArrowHeight * 2, kArrowHeight);
    CGContextAddLineToPoint(context, rect.size.width - kCornerRadius, kArrowHeight);
    
    CGContextAddArc(context, rect.size.width - kCornerRadius, kArrowHeight+kCornerRadius, kCornerRadius, -M_PI/2, 0.0, 0);
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height - kCornerRadius);
    
    CGContextAddArc(context, rect.size.width - kCornerRadius, rect.size.height - kCornerRadius, kCornerRadius, 0.0, M_PI/2.0, 0);
    CGContextAddLineToPoint(context, kCornerRadius, rect.size.height);
    
    CGContextAddArc(context, kCornerRadius, rect.size.height - kCornerRadius, kCornerRadius, M_PI/2.0, M_PI, 0);
    CGContextAddLineToPoint(context, 0, kCornerRadius+kArrowHeight);
    
    CGContextAddArc(context, kCornerRadius, kCornerRadius+kArrowHeight, kCornerRadius, M_PI, M_PI*1.5, 0);
    
    CGContextDrawPath(context, kCGPathFillStroke);
    
    [super drawRect:rect];
}

- (void)layoutSubviews
{
    float height = 0.0;
    if (self.isShowOrHide)
    {
        height = self.tableViewDatas.count * kCellHeight + kArrowHeight;
    }
    
    CGRect rect = self.frame;
    rect.size.height = height;
    self.frame = rect;
    
    rect = CGRectMake(6, kArrowHeight, rect.size.width - 10, rect.size.height - kArrowHeight);
    self.popoverTableView.frame = rect;
    
    [super layoutSubviews];
}

#pragma mark - protected methods

- (void)showPopoverView
{
    float height = self.tableViewDatas.count * kCellHeight + kArrowHeight * 2;
    CGRect rect = self.frame;
    rect.size.height = height;
    
    DLog(@"count:%d , height:%f",self.tableViewDatas.count,height);
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    
    self.frame = rect;
    rect = CGRectMake(7, kArrowHeight*1.5, rect.size.width - 10, rect.size.height - kArrowHeight*1.5);
    self.popoverTableView.frame = rect;
    
    [UIView commitAnimations];
    
    [self.popoverTableView reloadData];
}

- (void)dismissPopoverView
{
    CGRect rect = self.frame;
    rect.size.height = 0.0;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.1];
    
    self.frame = rect;
    self.popoverTableView.frame = rect;
    
    [UIView commitAnimations];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *popCellIdentifier = @"popCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:popCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:popCellIdentifier] autorelease];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        cell.textLabel.textColor = [UIColor whiteColor];
    }
    
    DLog(@"cell count:%d , row:%d",self.tableViewDatas.count,indexPath.row);
    
    KeyValueObject *item = [self.tableViewDatas objectAtIndex:indexPath.row];
    cell.textLabel.text = item.keyDispName;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate)
    {
        KeyValueObject *item = [self.tableViewDatas objectAtIndex:indexPath.row];
        [self.delegate returnChoosedKeyValue:item];
    }
}

@end